package com.example.elmspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.elmspringboot.controller.utils.Result;
import com.example.elmspringboot.domain.Cart;
import com.example.elmspringboot.service.CartService;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/cart")
public class CartController {
    @Resource
    private CartService cartService;
    @PostMapping("/add-item")
    public Result addItem(@RequestBody Cart cart){
        return new Result(true, cartService.saveCart(cart));
    }
//  获得所有的购物车食物
    @GetMapping("/get-cart-food")
    public Result getCartFood(@RequestParam Integer userId, @RequestParam Integer shopId){
        return new Result(true, cartService.getCartFood(userId, shopId));
    }
    // 移除购物车里的食物通过userId和shopId
    @DeleteMapping("/delete-cart")
    public Result deleteCart(@RequestParam Integer userId, @RequestParam Integer shopId){
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("shop_id", shopId);
        List<Cart> list = cartService.list(wrapper);
        for (Cart cart : list) {
            cartService.removeById(cart.getId());
        }
        return new Result(true);
    }
}
